{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 2. 数据统计函数",
   "id": "de697bf4893b0c2a"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-12T06:33:08.630149Z",
     "start_time": "2025-09-12T06:33:08.623521Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "path = 'D:/2506A/monty03/day15/file/'"
   ],
   "id": "7f3626ca03932fe7",
   "outputs": [],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-12T06:43:43.221075Z",
     "start_time": "2025-09-12T06:43:43.136472Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df = pd.read_excel(path + '成绩2.xlsx',index_col='序号')\n",
    "\n",
    "# 数据基本统计\n",
    "# print(df.describe())\n",
    "\n",
    "# 指定保留小数位\n",
    "# print(df.describe().round(2))\n",
    "\n",
    "# 统计指定列\n",
    "# print(df['机试'].describe().round(2))\n",
    "\n",
    "# 根据性别分类统计\n",
    "# print(df['性别'].describe().round(2))\n",
    "\n",
    "\n",
    "print(df.groupby('性别').describe())"
   ],
   "id": "3549e0abd8efbf74",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      理论                                                     机试        ...  \\\n",
      "   count  mean        std   min    25%   50%    75%   max count  mean  ...   \n",
      "性别                                                                     ...   \n",
      "女    4.0  79.0  11.575837  70.0  73.75  75.0  80.25  96.0   4.0  53.0  ...   \n",
      "男    4.0  65.0  23.508864  32.0  56.75  71.5  79.75  85.0   4.0  67.5  ...   \n",
      "\n",
      "                  品德                                                    \n",
      "     75%   max count   mean        std   min    25%   50%    75%   max  \n",
      "性别                                                                      \n",
      "女   58.5  99.0   4.0  53.75  40.260609  15.0  21.00  55.5  88.25  89.0  \n",
      "男   78.0  78.0   4.0  60.75  28.814059  33.0  41.25  56.0  75.50  98.0  \n",
      "\n",
      "[2 rows x 24 columns]\n"
     ]
    }
   ],
   "execution_count": 15
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-12T06:46:10.997005Z",
     "start_time": "2025-09-12T06:46:10.975404Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 特定统计指标\n",
    "print(df.agg({\n",
    "    '机试': ['mean', 'std', 'min', 'max'],\n",
    "    '理论': ['median', 'var', 'skew'],\n",
    "    '品德': ['count', 'quantile']\n",
    "}))"
   ],
   "id": "3afa91cce84f26bc",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                 机试          理论    品德\n",
      "mean      60.250000         NaN   NaN\n",
      "std       24.081706         NaN   NaN\n",
      "min       32.000000         NaN   NaN\n",
      "max       99.000000         NaN   NaN\n",
      "median          NaN   75.000000   NaN\n",
      "var             NaN  350.285714   NaN\n",
      "skew            NaN   -1.396339   NaN\n",
      "count           NaN         NaN   8.0\n",
      "quantile        NaN         NaN  56.0\n"
     ]
    }
   ],
   "execution_count": 16
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
